package com.jianyun.jyzs.widget;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import com.jianyun.jyzs.R;
import com.jianyun.jyzs.bean.EngineerAttach;
import com.jianyun.jyzs.utils.Const;
import com.jrmf360.rylib.common.util.ToastUtil;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import com.jianyun.jyzs.utils.RceLog;

/**
 * Created by mlli on 2017/11/1.
 * 用来显示工程动态的附件 及添加
 */

public class EngineerAttachExpandView {

    private static AttachRecycler attachRecycler;
    private static Context mContext;
    static EngineerAttDialog.OnDialogItemClickListener listener;

    public void setListener(EngineerAttDialog.OnDialogItemClickListener listener) {
        this.listener = listener;
    }



    public static EngineerAttachExpandView init(Context context, RecyclerView recyclerView, List<EngineerAttach> atts) {
        mContext = context;
        recyclerView.setLayoutManager(new GridLayoutManager(context, 4));
        attachRecycler = new AttachRecycler(context);
        recyclerView.setAdapter(attachRecycler);
        if (atts.size() == 0) {
            EngineerAttach attach = new EngineerAttach(0, null, null);
            atts.add(attach);
            attachRecycler.setList(atts);
        }
        return new EngineerAttachExpandView();
    }

    //得到需要上传的附件集合
    public List<EngineerAttach> getAttachList(){
        List<EngineerAttach> list = attachRecycler.getList();
        List<EngineerAttach> list2 = new ArrayList<>();
        for (EngineerAttach ea:list) {
            if (ea.getCategory()==-1)break;
            if (ea.getCategory()==0)break;
            list2.add(ea);
        }
        return list2;
    }

    public static void setAttach(List<EngineerAttach> atts) {
        List<EngineerAttach> list = attachRecycler.getList();
        if (list.size() == 1) {
            EngineerAttach attach0 = new EngineerAttach(0, null, null);
            EngineerAttach attach1 = new EngineerAttach(-1, null, null);
            atts.add(atts.size(), attach0);
            atts.add(atts.size(), attach1);
        } else {
            atts.addAll(list);
        }
        if (atts.size()>11){
            ToastUtil.showNoWaitToast(mContext,"动态附件不能超过9个");
            atts.subList(atts.size()-11,atts.size());
        }
        //如果长度超过9个截取 提示
        attachRecycler.setList(atts);
    }

    static class AttachRecycler extends RecyclerView.Adapter {
        private Context context;
        private List<EngineerAttach> list;
        private boolean isdelMode = false;

        public List<EngineerAttach> getList() {
            return list;
        }

        public void setList(List<EngineerAttach> list) {
            this.list = list;
            notifyDataSetChanged();
        }

        public AttachRecycler(Context context) {
            this.context = context;

        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = View.inflate(context, R.layout.image_item, null);
            return new MHolder(view);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            MHolder mHolder = (MHolder) holder;
            EngineerAttach bean = list.get(position);
            mHolder.del.setVisibility(View.GONE);
            mHolder.play.setBackgroundResource(0);
            if (bean.getCategory()!=0&bean.getCategory()!=-1){
                if (isdelMode)mHolder.del.setVisibility(View.VISIBLE);
            }

            mHolder.del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ToastUtil.showNoWaitToast(context, "del"+position);
                    list.remove(position);
                    notifyItemRemoved(position);
                }
            });
            if (bean.getCategory() == 0) {
                Picasso.with(context).load(R.drawable.gongchengdongtaic_icon_tianjia_pressed).into(mHolder.image);
                mHolder.image.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        EngineerAttDialog dialog = new EngineerAttDialog(context);
                        dialog.show();
                        dialog.setListener(new EngineerAttDialog.OnDialogItemClickListener() {
                            @Override
                            public void onItemClick(int index) {
                                listener.onItemClick(index);
                            }
                        });
                    }
                });
            } else if (bean.getCategory() == -1) {
                Picasso.with(context).load(R.drawable.gongchengdongtaic_icon_jiashao_pressed).into(mHolder.image);
                mHolder.image.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        isdelMode=!isdelMode;
                        notifyDataSetChanged();
                    }
                });
            } else if (bean.getCategory() == 1) {
                String path = bean.getPath();
//                Picasso.with(context).load(path).into(mHolder.image);
                Glide.with(context).load(path).into(mHolder.image);
                mHolder.image.setOnClickListener(new BigImageViewClickListener(path));

            } else if (bean.getCategory() == 2) {
                final String path = bean.getPath();
                Picasso.with(context).load(R.drawable.gongchengdongtaic_icon_yuyin_default).into(mHolder.image);
                mHolder.image.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(Uri.parse(path),"audio/mp3");
                        context.startActivity(intent);
                    }
                });
            }else if (bean.getCategory()==3){
                final String path = bean.getPath();
                RceLog.d("lml",path.toString());
                Glide.with(context).load(path).into(mHolder.image);
                mHolder.play.setBackgroundResource(R.drawable.gongchengdongtaic_icon_shipin_default);
                mHolder.image.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(Uri.parse(path),"video/mp4");
                        context.startActivity(intent);
                    }
                });
            }

        }

        @Override
        public int getItemCount() {
            return list == null ? 0 : list.size();
        }

        class MHolder extends RecyclerView.ViewHolder {

            private final ImageView image;
            private final ImageView del;
            private final ImageView play;

            public MHolder(View itemView) {
                super(itemView);
                image = (ImageView) itemView.findViewById(R.id.itemImage);
                del = (ImageView) itemView.findViewById(R.id.del);
                play = (ImageView) itemView.findViewById(R.id.play);
            }
        }
        class BigImageViewClickListener implements View.OnClickListener {

            private String path;

            public BigImageViewClickListener(String path) {
                this.path = path;
            }

            @Override
            public void onClick(View view) {
                /**
                 * TODO 待完成
                 */
//                Intent intent = new Intent(context, UserBigPortraitActivity.class);
//                intent.putExtra(Const.PORTRAIT, path);
//                context.startActivity(intent);

            }
        }
    }

}
